package cn.studyjams.s1.contest.autohello;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

import cn.studyjams.s1.contest.autohello.utils.RegexValidateUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class SignInFragment extends Fragment implements View.OnClickListener {


    // UI references.
    private AutoCompleteTextView mEmailView;
    private EditText mPasswordView;

    SignInListener signInListener;

    public interface SignInListener {
        void onSignIn(String email, String pwd);

        void onSignUpButton();
    }

    public SignInFragment() {
        // Required empty public constructor
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            signInListener = (SignInListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + "must implement SignInFragment");
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sign_in, container, false);

        mEmailView = (AutoCompleteTextView) view.findViewById(R.id.email);
        mPasswordView = (EditText) view.findViewById(R.id.password);

        view.findViewById(R.id.email_sign_in_button).setOnClickListener(this);
        view.findViewById(R.id.email_sign_up_button).setOnClickListener(this);
        // view.findViewById(R.id.google_sign_in_button).setOnClickListener(this);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onClick(View view) {
        SignInListener listener = (SignInListener) getActivity();
        switch (view.getId()) {
            case R.id.email_sign_in_button:
                if (check()) {
                    listener.onSignIn(
                            mEmailView.getText().toString(),
                            mPasswordView.getText().toString()
                    );
                }
                break;
            case R.id.email_sign_up_button:
                listener.onSignUpButton();
                break;
            default:
                break;
        }
    }

    private boolean isEmailValid(String email) {
        return RegexValidateUtil.isEmailValid(email);
    }

    private boolean isPasswordValid(String password) {
        return RegexValidateUtil.isPasswordValid(password);
    }

    private boolean check() {

        // Reset errors.
        mEmailView.setError(null);
        mPasswordView.setError(null);

        // Store values at the time of the login attempt.
        String email = mEmailView.getText().toString();
        String password = mPasswordView.getText().toString();

        // Check for a valid email address.
        if (TextUtils.isEmpty(email)) {
            mEmailView.setError(getString(R.string.required));
            return false;
        } else if (!isEmailValid(email)) {
            mEmailView.setError(getString(R.string.error_invalid_email));
            return false;
        }

        // Check for a valid password, if the user entered one.
        if (TextUtils.isEmpty(password)) {
            mPasswordView.setError(getString(R.string.required));
            return false;
        } else if (!isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_invalid_password));
            return false;
        }
        return true;
    }
}
